﻿@page "/app/settings/scanning"
@using RatScanner.Components;
@inject SettingsVM SettingsVM
@inject IJSRuntime JSRuntime
@implements IDisposable
@using System.Linq

<div>
    <MudGrid Class="pa-0 ma-0" Spacing="0" Style="height: 100%;">
        <MudItem xs="12">
            <div class="d-flex align-center justify-center mx-2 mb-1">
                <MudText Typo="Typo.subtitle1" Color="Color.Inherit">Scanning Settings</MudText>
            </div>
        </MudItem>
        <MudItem xs="12">
            <MudPaper Class="align-center justify-center pa-2 mx-2 mb-2" Elevation="2">
                <MudGrid Spacing="0" Class="pa-0 ma-0">
                    <MudSelect @bind-Value="@SettingsVM.NameScanLanguage" T="int" Label="Name Scan Language" AnchorOrigin="Origin.BottomCenter" Variant="Variant.Text" Margin="Margin.Dense" Dense="true" Class="mx-1">
                        @foreach (var language in GetLanguages())
                        {
                            <MudSelectItem T="int" Value="@language.ordinal">
                                @language.name
                            </MudSelectItem>
                        }
                    </MudSelect>
                    <MudCheckBox @bind-Checked="@SettingsVM.EnableNameScan" Label="Enable Name Scan" Dense="true" Size="Size.Small" Class="pa-0 ma-0"></MudCheckBox>
                    <MudCheckBox @bind-Checked="@SettingsVM.EnableAutoNameScan" Label="Automatic Scanning" Dense="true" Size="Size.Small" Class="pa-0 ma-0"></MudCheckBox>
                </MudGrid>
            </MudPaper>
        </MudItem>
        <MudItem xs="12">
            <MudPaper Class="align-center justify-center pa-2 mx-2 mb-2" Elevation="2">
                <MudStack Spacing="0" Class="pa-0 ma-0">
                    <MudCheckBox @bind-Checked="@SettingsVM.EnableIconScan" Label="Enable Icon Scan" Dense="true" Size="Size.Small" Class="pa-0 ma-0"></MudCheckBox>
                    <MudCheckBox @bind-Checked="@SettingsVM.ScanRotatedIcons" Label="Scan Rotated Icons" Dense="true" Size="Size.Small" Class="pa-0 ma-0"></MudCheckBox>
                    <MudCheckBox @bind-Checked="@SettingsVM.UseCachedIcons" Label="Use Cached Icons" Dense="true" Size="Size.Small" Class="pa-0 ma-0"></MudCheckBox>
                </MudStack>
            </MudPaper>
        </MudItem>
        <MudItem xs="12">
            <MudPaper Class="align-center justify-center pa-2 mx-2 mb-2" Elevation="2">
                <MudStack Spacing="0" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center" Row="true">
                    <MudItem>
                        <MudText>
                            Icon Scan Hotkey
                        </MudText>
                    </MudItem>
                    <MudItem>
                        <HotkeySelector Hotkey="@SettingsVM.IconScanHotkey"></HotkeySelector>
                    </MudItem>
                </MudStack>
            </MudPaper>
        </MudItem>
    </MudGrid>
</div>

@code {

    private (string name, int ordinal)[] GetLanguages()
    {
        // Sort all elements of the enum RatStash.Language
        var values = Enum.GetValues(typeof(RatStash.Language)).Cast<RatStash.Language>();
        var languages = values.Select(language => (name: language.ToString(), ordinal: (int)language));
        return languages.OrderBy(language => language.name).ToArray();
    }

    protected override void OnInitialized()
    {
        SettingsVM.PropertyChanged += PropertyChangeHandler;
    }

    private async void PropertyChangeHandler(object? sender, EventArgs e)
    {
        await InvokeAsync(() => StateHasChanged());
    }

    public void Dispose()
    {
        SettingsVM.PropertyChanged -= PropertyChangeHandler;
    }

    public async Task LogAsync(string message)
    {
        await JSRuntime.InvokeVoidAsync("console.log", message);
    }
}
